package vn.edu.ptithcm.pim.controller;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import vn.edu.ptithcm.pim.common.bean.SendMailObject;
import vn.edu.ptithcm.pim.constant.Constants;
import vn.edu.ptithcm.pim.constant.Locales;
import vn.edu.ptithcm.pim.constant.UrlConstants;
import vn.edu.ptithcm.pim.dom.User;
import vn.edu.ptithcm.pim.service.SendMailService;
import vn.edu.ptithcm.pim.service.UserService;
import vn.edu.ptithcm.pim.util.ParamUtil;

@SuppressWarnings("deprecation")
public class ResetPasswordController extends SimpleFormController {
	private UserService userService;
	private LocaleResolver localeResolver;
	private SendMailService sendMailService;

	public ResetPasswordController() {
		super();
		setSessionForm(true);
		setCommandName("noname");
		setCommandClass(Object.class);
		setFormView(UrlConstants.RESET_PASSWORD_URL);
		setSuccessView(UrlConstants.RESET_PASSWORD_URL);
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		return new Object();
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		String email = ParamUtil.getString(request, "email", "");
		if (!email.isEmpty()) {
			User user = userService.findByEmail(email);
			if (user != null) {
				String newPassword = userService.resetPassword(email, null);
				SendMailObject sendMailObject = new SendMailObject();

				sendMailObject.setSuject("Pim - Reset Your Password");
				sendMailObject.setFrom(Constants.MAIL_HOST_SERVER);

				Map<String, Object> data = new HashMap<String, Object>();
				data.put("clearPassword", newPassword);
				data.put("visa", user.getEmployee().getVisa());

				String confirmUrl = request.getRequestURL().toString()
						.replaceAll(request.getServletPath(), "/homepage.htm");
				data.put("confirmUrl", confirmUrl);

				sendMailObject.setData(data);

				Locale locale = getLocaleResolver().resolveLocale(request);
				if (Locales.VI.equals(locale.toString())) {
					sendMailObject.setTemplate("vi_templates/resetPassword.vm");
				} else if (Locales.EN.equals(locale.toString())) {
					sendMailObject.setTemplate("en_templates/resetPassword.vm");
				}

				sendMailObject.setTo(user.getEmail());
				//sendMailService.send(sendMailObject);

				request.setAttribute("resetOK", true);
			} else {
				request.setAttribute("resetOK", false);
			}
			request.setAttribute("email", email);
		}
		return showForm(request, response, errors);
	}

	@Override
	protected ModelAndView showForm(HttpServletRequest request,
			HttpServletResponse response, BindException errors)
			throws Exception {
		String locale = ParamUtil.getString(request, "locale", "");
		if (!locale.isEmpty()) {
			getLocaleResolver()
					.setLocale(request, response, new Locale(locale));
		} else {
			locale = getLocaleResolver().resolveLocale(request).getLanguage();
		}
		request.setAttribute("locale", locale);
		return super.showForm(request, response, errors);
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public LocaleResolver getLocaleResolver() {
		return localeResolver;
	}

	public void setLocaleResolver(LocaleResolver localeResolver) {
		this.localeResolver = localeResolver;
	}

	public SendMailService getSendMailService() {
		return sendMailService;
	}

	public void setSendMailService(SendMailService sendMailService) {
		this.sendMailService = sendMailService;
	}

}
